﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using RFIDStockManagement.Model.InterFace;
using RFIDStockManagement.Model.Public;
using System.Data.Common;

namespace RFIDStockManagement.DAL.Reports
{
    public class DBReportBase : IReport
    {
        private List<ReportColumn> _columns;
        private string filterKey;
        private string reportName;
        private Dictionary<string, object> filters;
        private DbHelper _db;
        private string key;

        public DBReportBase()
        {
            _db = new DbHelper();
            _columns = new List<ReportColumn>();
        }

        /// <summary>
        /// 用于取数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        protected DataTable GetData(string sql)
        {
            DbCommand cmd = _db.GetSqlStringCommand(sql);

            DataTable dt = _db.ExecuteDataTable(cmd);

            return dt;
        }


        public void AddColumn(ReportColumn column)
        {
            _columns.Add(column);
        }

        /// <summary>
        /// 添加过滤条件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddFilterData(string key, object value)
        {
            filters[key] = value;
        }

        /// <summary>
        /// 获取当前过滤条件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetFilterData(string key)
        {
            object tryOjb;
            if (filters.TryGetValue(key, out tryOjb))
            {
                return tryOjb.ToString();
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取时间类型的过滤条件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="fomater"></param>
        /// <returns></returns>
        public string GetFilterData(string key, string fomater)
        {
            object tryObject;
            DateTime filterTime;
            if (filters.TryGetValue(key, out tryObject))
            {

                if (tryObject is DateTime)
                {
                    filterTime = (DateTime)tryObject;
                    return filterTime.ToString(fomater);
                }
                return tryObject.ToString();
            }

            return string.Empty;
        }

        public ReportColumn AddColumn(string fieldName, string caption)
        {
            ReportColumn newColumn = new ReportColumn(fieldName, caption);
            _columns.Add(newColumn);

            return newColumn;
        }

        #region IReport 成员

        public virtual string FilterKey
        {
            get
            {
                return filterKey;
            }
            set
            {
                filterKey = value;
            }
        }

        public virtual System.Data.DataTable GetReportData()
        {
            return new DataTable() ;
        }



        public virtual List<ReportColumn> Columns
        {
            get
            {
                return _columns;
            }
            set
            {
                _columns = value;
            }
        }



        public virtual string ReportCaption
        {
            get
            {
                return reportName;
            }
            set
            {
                reportName = value;
            }
        }


        public Dictionary<string, object> Filters
        {
            get
            {
                return filters;
            }
            set
            {
                filters = value;
            }
        }


        public string ReportKey
        {
            get
            {
                return key; 
            }
            set
            {
                key = value;
            }
        }

        #endregion
    }
}
